package com.key.puzzlemodel

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.key.puzzlemodel.config.PuzzleConfig
import com.key.puzzlemodel.databinding.ActivitySaveBitmapBinding
import com.key.puzzlemodel.mvvm.BaseMVVMActivity
import com.key.puzzlemodel.mvvm.BaseMVVMViewModel
import com.key.puzzlemodel.util.BitmapUtil
import com.key.puzzlemodel.util.PuzzleAppManager


/**
 *
 * @功能描述:     保存图片界面
 * @创建时间:     2022/1/4
 * @创建者:       阳
 * @修改时间:     2022/1/4
 * @修改者:       阳
 */
class SaveBitmapActivity : BaseMVVMActivity<ActivitySaveBitmapBinding, BaseMVVMViewModel>() {

    val TAG = "SaveBitmapActivity"

    override val layoutId: Int
        get() = R.layout.activity_save_bitmap

    override fun initView(savedInstanceState: Bundle?) {

        //添加activity管理
        PuzzleAppManager.getInstance().addActivity(this)

        if (PuzzleConfig.saveBitmap != null) {
//            Glide.with(this).load(PuzzleConfig.saveBitmap).into(mVDB.imgSave)
            mVDB.imgSave.setImageBimap(PuzzleConfig.saveBitmap!!)
        }


        findViewById<TextView>(R.id.txt_top_title).text = getString(R.string.page_save_bitmap)
        findViewById<ImageView>(R.id.image_return).setOnClickListener {
            onBackPressed()
        }

        //保存
        val txtSave = findViewById<TextView>(R.id.txt_top_title_right)
        txtSave.visibility = View.VISIBLE
        txtSave.text = getString(R.string.str_puzzle_export)
        txtSave.setOnClickListener {
            BitmapUtil.savePuzzleBitmap(this, PuzzleConfig.saveBitmap!!) { state, photoPath ->

                if (state) {
                    PuzzleConfig.saveBitmap!!.recycle()
                    PuzzleConfig.saveBitmap = null
                    //Toast.makeText(this, "已成功保存到相册", Toast.LENGTH_SHORT).show()
                   // Log.e("返回数据：", "SaveBitmapActivity == $photoPath")
                    //返回图片的地址
                    setResult(Activity.RESULT_OK, Intent().apply {
                        putExtra(PuzzleConfig.EDIT_RESULT, photoPath)
                    })
                    this.finish()
                } else {
                    Toast.makeText(this, "保存失败，请重试", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

}